<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<title>发药</title>
</head>
<body>
<script>
	$(function(){		
		
		$("#yzr-grantMed-chufang").combogrid({onChange: function(newValue, oldValue){	
			
			//查出病人姓名
			var chufangId = $("#yzr-grantMed-chufang").combogrid("getValue");			
			$.getJSON("findBingRenNameYzr",{"chufangId":chufangId}, function(data){
				$("#yzr-binrenName").textbox('setValue',data);	
			});
			
			//查出处方下的药品明细
			$("#yzr-grantMed-medDet").datagrid({
				url:'queryAllMedDetYzrYzr',
				queryParams:{
	    			chufangId: chufangId
				},
			 	onLoadSuccess:function(data){  
		            //要判断或者执行的代码  
			 		var rows = $("#yzr-grantMed-medDet").datagrid("getRows");
		            var mm=0;
		            for(var i=0;i<rows.length;i++){
		            	mm = mm + rows[i].money;
		            }
		            $("#yzr-grantMed-number").numberbox('setValue',mm);		            
		        }
			});
			
			var yaofang = $("#yzr_grantMed_kind").combobox('getText');
			$("#yzr-grantMed-pharmacy").combogrid('setValue','YF-2017031900003');			
			
		}});
		
		//提交数据
		$("#yzr-grantMed-submit").linkbutton({onClick: function(){
			
			var  grant_staffId = $("#tontyiStaffId").html();
			var yaofangkind = $("#yzr_grantMed_kind").combobox('getText');
			var yf = $("#yzr-grantMed-pharmacy").combogrid('getValue');
			var zje = $("#yzr-grantMed-number").numberbox('getValue');
			var chufangId = $("#yzr-grantMed-chufang").combogrid("getValue"); 
			var br = $("#yzr-binrenName").textbox("getValue");
			
			
			
			var rows = $("#yzr-grantMed-medDet").datagrid("getRows");
			console.log(JSON.stringify(rows));
			$("<form id='yzr_addMedGrant_form_2' method='post'>").appendTo($("body"));
			
			for(var i=0;i<rows.length;i++){
				generateHiddenYzr_grant(rows[i], "medId", i);
				generateHiddenYzr_grant(rows[i], "medName", i);
				generateHiddenYzr_grant(rows[i], "pihao", i);
				generateHiddenYzr_grant(rows[i], "number", i);
				generateHiddenYzr_grant(rows[i], "salePrice", i);
				generateHiddenYzr_grant(rows[i], "money", i);
			}
			
			$('#yzr_addMedGrant_form_2').form('submit', {				
				url: 'addGrantYzr',
				queryParams: {
					staffId: grant_staffId,
					yaofangkind: yaofangkind,
					yf: yf,
					zje: zje,
					chufangId:chufangId,
					br:br
	    		}, 
				success: function(data){
					
					$.messager.alert('提示', "提交成功");
										
					$(":hidden[x='y']").remove();			
					$('#yzr-grant-form').form("clear");
					$('#yzr-grantMed-medDet').datagrid('loadData', { total: 0, rows: [] }); 
				}
			});
			
			
		}});
		
		function generateHiddenYzr_grant(row, name, i){
			var ff_plan = $('#yzr_addMedGrant_form_2');
			//type='hidden'
			$("<input  x='y'>")
				.attr("name", "medList["+i+"]."+name)
				.val(row[name])
				.appendTo(ff_plan);
		}
		
		
		
		//查看详情
		$("#yzr-grant-detail-more").linkbutton({onClick: function(){
			var rows = $("#yzr-grant-detail-dg").datagrid("getSelections");
			if(rows.length == 1){
				var grantMedId = rows[0].grantMedId;
				$('#yzr-grantMed-detail-window').window("open");
				
				$.getJSON("queryGrantByIdYzr", {"grantMedId": grantMedId}, function(data){
					//grantMedId,grantMedBingrenName,grantMedCaozuoyuan,grantMedChufangId,grantMedDate,grantMedKind,grantMedMoney,grantMedPharmacyId
					$("#yzr-grantMed-detail-fybh").linkbutton({text: data.grantMedId});
					$("#yzr-grantMed-detail-fylx").linkbutton({text: data.grantMedKind});
					$("#yzr-grantMed-detail-chdh").linkbutton({text: data.grantMedChufangId});
					$("#yzr-grantMed-detail-brxm").linkbutton({text: data.grantMedBingrenName});
					$("#yzr-grantMed-detail-czy").linkbutton({text: data.staffName});
					$("#yzr-grantMed-detail-yf").linkbutton({text: data.pharName});
					$("#yzr-grantMed-detail-zje").linkbutton({text: data.grantMedMoney});
					$("#yzr-grantMed-detail-fyrq").linkbutton({text: data.grantMedDate});
					
				});	
				
				 //根据id查看详情	
				 $("#yzr-grantMed-detail-dg").datagrid({
					url:'queryDetByGrantIdYzr',
					width: '100%',
					queryParams:{
						grantMedId: grantMedId
					}
				});
			}else{
				$.messager.alert("提示", "请选择一条记录!");
			}
		}});
		
		//刷新
		$("#yzr-grant-detail-reload").linkbutton({onClick: function(){
			$("#yzr-grant-detail-dg").datagrid({
				url:'queryAllGrantstockYzr',			
			});
		}});
		
		
		$('#yzr-grant-tabs').tabs({
		    onSelect:function(title,index){
		        
		        if(title == '发药明细'){
		        	$("#yzr-display-grant-none").show();
		        }else if(title == '查看发药详情'){
		        	$("#yzr-display-grant-none").css("display","none");
		        }
		    }
		});
	});

	
</script>
	<div id="yzr-display-grant-none">
	<form style="padding:20px;background:#ECF3FF;" id="yzr-grant-form">
		<table style="width:90%;">
			<tr>
				<td style="width:35%;">
					<select id="yzr_grantMed_kind" class="easyui-combobox" data-options="
					   		label: '发药类型',
							labelAlign:'right',
							required:true,
							width: '88%',
							panelWidth: '150',
							panelHeight: 'auto'">
					    <option value="住院发药">住院发药</option>
					    <option value="门诊发药">门诊发药</option>
					</select>
				</td>
				<td style="width:35%;">
					<select class="easyui-combogrid" id="yzr-grantMed-pharmacy"
						data-options="
							label: '药房',
							labelAlign:'right',
							required:true,
							width: '88%',
							panelWidth: '290',
							panelHeight: 'auto',
							url: 'queryAllPharmacy',
							pagination: true,
							pageSize: 8,
							pageList: [8, 16, 24, 32],
							idField: 'pharmacyId',
							textField: 'pharmacyName',
							columns: [[				
								{field:'pharmacyName',title:'药房名',width:100},
								{field:'staffName',title:'负责人',width:85},
								{field:'pharmacyAddress',title:'地址',width:100}
							]]
					"></select>
				</td>
				<td style="width:30%;">
					<input id="yzr-grantMed-number" class="easyui-numberbox" data-options="required:true,width: '95%',labelAlign:'right',label:'总金额'"/>
				</td>
			</tr>
			<tr>
				<td>
					<select class="easyui-combogrid" id="yzr-grantMed-chufang"
						data-options="
							label: '处方号',
							labelAlign:'right',
							required:true,
							width: '88%',
							panelWidth: '290',
							panelHeight: 'auto',
							url: 'queryByNoSuccessYzr',
							pagination: true,
							pageSize: 8,
							pageList: [8, 16, 24, 32],
							idField: 'heId',
							textField: 'pharmacyName',
							columns: [[				
								{field:'heId',title:'处方号',width:100},
								{field:'heSickid',title:'病人',width:85},
								{field:'hospeleRemarks',title:'备注',width:100}
							]]
					"></select>
				</td>
				<td>
					<input id="yzr-binrenName" class="easyui-textbox" data-options="required:true,width: '88%',labelAlign:'right',label:'病人姓名'"/>
				</td>
			</tr>
		</table>
	</form>
	
	</div>
	
	
	
	<div style="margin: 20px;">
		<div id="yzr-grant-tabs" class="easyui-tabs" style="width:100%; margin-top:10px;" 
			data-options="plain:true,narrow:true,tabHeight:'150%',tabWidth:'100%'">
			
			<!-- 药品编号、药品名称、数量、单价、金额、单位、批号 -->
			<!-- tab1  -->
			<div title="发药明细" style="padding:10px;">
				<table id="yzr-grantMed-medDet" class="easyui-datagrid"  
						data-options="
							iconCls: 'icon-edit',
							singleSelect:true,	
							toolbar: '#yzr-grantMed-toolbar',					
							width: '98%'
							">
						<thead>
							<tr>
								<th data-options="field:'medId',width:'25%',align:'center',hidden:true">药品名称</th>	
								<th data-options="field:'medName',width:'24%',align:'center'">药品名称</th>
								<th data-options="field:'pihao',width:'26%',align:'center'">批号</th>
								<th data-options="field:'stockNumber',width:'10%',align:'center'">库存数量</th>
								<th data-options="field:'number',width:'10%',align:'center'">数量</th>			
								<th data-options="field:'salePrice',width:'15%',align:'center'">单价</th>
								<th data-options="field:'money',width:'15%',align:'center'">金额</th>
							</tr>
						</thead>
				</table>
				<div id="yzr-grantMed-toolbar">
					<a id="yzr-grantMed-submit" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-ok'">确定</a>
				</div>
			</div>
			
			
	
	
	
	
			<!-- tab2 -->
			<div  title="查看发药详情" style="padding:10px;">
				<table id="yzr-grant-detail-dg" class="easyui-datagrid" 
					data-options="
						url: 'queryAllGrantstockYzr',
						iconCls: 'icon-edit',	
						pagination: true,
						singleSelect: true,
						toolbar: '#yzr-grant-detail-tb',
						pageSize: 16,
						pageList: [16, 32, 48, 54],						
						width: '100%',
						height: '400px'
						">
					<thead>
						<tr>
						<!--  发药编号，发药日期，发药类型（门诊/药房），药房，操作员，总金额/处方、病人姓名
			 				  grantMedId,grantMedBingrenName,grantMedCaozuoyuan,grantMedChufangId,grantMedDate,grantMedKind,grantMedMoney,grantMedPharmacyId -->	
							<th data-options="field:'XXX',width:'18%',checkbox:true">入库编号</th>
							<th data-options="field:'grantMedId',width:'18%',align:'center'">发药编号</th>				
							<th data-options="field:'grantMedKind',width:'15%',align:'center'">发药类型</th>
							<th data-options="field:'pharName',width:'16%',align:'center'">药房</th>
							<th data-options="field:'grantMedChufangId',width:'14%',align:'center'">处方号</th>
							<th data-options="field:'grantMedBingrenName',width:'12%',align:'center'">病人姓名</th>
							<th data-options="field:'staffName',width:'12%',align:'center'">操作员</th>
							<th data-options="field:'grantMedMoney',width:'10%',align:'center'">总金额</th>														
						</tr>
					</thead>
				</table>
				<div id="yzr-grant-detail-tb">
					<a id="yzr-grant-detail-more" class="easyui-linkbutton" data-options="iconCls:'icon-more',plain:true">查看详情</a>
					<a id="yzr-grant-detail-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>					
				</div> 
			</div>	
			
				
		</div>						
	</div>
	
	
	<!--查看详情窗口 -->
	<div id="yzr-grantMed-detail-window" class="easyui-window" title="查看发药详情" data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true" style="width:70%;height:70%;">		
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">			
			<div data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">	
				
				<form id="yzr_grantMed-detail_form" method="post" style="margin:10px;width:98%;">
					<table width="98%" style="margin:10px;">
						<tr>
							<td>发药编号:<a id="yzr-grantMed-detail-fybh" class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>发药类型:<a id="yzr-grantMed-detail-fylx"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>处方单号:<a id="yzr-grantMed-detail-chdh"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>病人姓名:<a id="yzr-grantMed-detail-brxm"  class="easyui-linkbutton" data-options="plain:true"></a></td>
						</tr>
						<tr>
							<td>操作员:<a id="yzr-grantMed-detail-czy"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>药房:<a id="yzr-grantMed-detail-yf"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>总金额:<a id="yzr-grantMed-detail-zje"  class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>发药日期:<a id="yzr-grantMed-detail-fyrq"  class="easyui-linkbutton" data-options="plain:true"></a></td>
						</tr>
					</table>
						
					<div style="margin: 10px;">
						<div id="yzr-grantMed-tabs" class="easyui-tabs" style="width:98%; margin-top:10px;" 
							data-options="plain:true,narrow:true">
							
							<div title="发药药品明细" style="padding:10px;">
								<table id="yzr-grantMed-detail-dg" class="easyui-datagrid"  style="margin-left:10px"
										data-options="
											iconCls: 'icon-edit',
											singleSelect: true,
											align: 'center',										
											width: '98%'
											">
										<thead>
											<tr>	<!-- 发药详情单号，发药编号，药品名称，批号，单位，规格，单价（卖出价格），数量，金额，备注	
													//grantMedDetId,grantMedDetBatchnumber,grantMedDetGrantMedId,grantMedDetGuige,grantMedDetMedId,
													grantMedDetMedName,grantMedDetMoney,grantMedDetNumber,grantMedDetPrice,grantMedDetRemark,grantMedDetUnit -->
												<th data-options="field:'grantMedDetId',width:'25%',align:'center'">发药详情编号</th>		
												<th data-options="field:'grantMedDetMedName',width:'25%',align:'center'">药品名称</th>
												<th data-options="field:'grantMedDetBatchnumber',width:'15%',align:'center'">批号</th>
												<!-- <th data-options="field:'grantMedDetGuige',width:'12%',align:'center'">规格</th> -->
												<th data-options="field:'grantMedDetPrice',width:'12%',align:'center'">单价</th>
												<th data-options="field:'grantMedDetNumber',width:'12%',align:'center'">数量</th>
												<th data-options="field:'grantMedDetMoney',width:'11%',align:'center'">金额</th>
											</tr>
										</thead>
								 </table>
							</div>						
						</div>				
					</div>
				</form>								
			</div>
					
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#yzr-grantMed-detail-window').window('close');" onclick="">取消</a>
			</div>			
		</div>
	</div>
	
</body>
</html>